package com.hmall.user.common;

import com.hmall.common.utils.LogUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class RequestLoggingAspect {

    @Before("execution(* com.hmall.user.controller.*.*(..))")
    public void logRequest(JoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String path = request.getRequestURI();
        Object[] args = joinPoint.getArgs();
        String method = request.getMethod();
        LogUtil.logRequest(path,method, args);
    }

    @AfterReturning(pointcut = "execution(* com.hmall.user.controller..*(..))", returning = "returnValue")
    public void logResponse(JoinPoint joinPoint, Object returnValue) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String path = request.getRequestURI();
        String method = request.getMethod();
        LogUtil.logResponse(path, method,returnValue);
    }
}